﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊订单记录
    /// </summary>
    public class OtpOrdRecService : ApplicationService, IOtpOrdRecService
    {
        private OtpOrdRecManager _otpOrdRecManager;
        private IOtpRxRecService _otpRxRecService;
        private IRepository<OtpOrdRec, Guid> _otpOrdRecRepository;
        private IOtpInvoService _otpInvoService;

        public OtpOrdRecService(OtpOrdRecManager otpOrdRecManager
            , IOtpRxRecService otpRxRecService
            , IRepository<OtpOrdRec, Guid> otpOrdRecRepository
            , IOtpInvoService otpInvoService)
        {
            _otpOrdRecManager = otpOrdRecManager;
            _otpRxRecService = otpRxRecService;
            _otpOrdRecRepository = otpOrdRecRepository;
            _otpInvoService = otpInvoService;
        }

        public async Task<PagedResultDto<OtpOrdRecDto>> GetListAsync(OtpOrdRecInput input)
        {
            var iQuery = await _otpOrdRecRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Invono), d => d.Invono == input.Invono);
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.OrdNo), d => d.OrdNo == input.OrdNo);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OtpOrdRec>, List<OtpOrdRecDto>>(iQuery.ToList());

                return new PagedResultDto<OtpOrdRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpOrdRec>, List<OtpOrdRecDto>>(items.ToList());

                return new PagedResultDto<OtpOrdRecDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<OtpOrdRecDto> GetAsync(Guid id)
        {
            var otpOrdRec = await _otpOrdRecRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<OtpOrdRec, OtpOrdRecDto>(otpOrdRec);

            return itemDto;
        }

        /// <summary>
        /// 创建门诊订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<OtpOrdRecDto> PostAsync(OtpOrdRecCreateDto input)
        {
            OtpOrdRec otpOrdRec = new OtpOrdRec();
            var otpInvno = await _otpInvoService.GetAsync();
            otpOrdRec.Invono = otpInvno.CurnInvono;
            otpOrdRec.InvoAmt = input.TotAmt;
            otpOrdRec.OrdBizSouc = input.OrdBizSouc;
            otpOrdRec.SetlTypeCode = input.SetlTypeCode;
            otpOrdRec.SetlTypeName = input.SetlTypeName;
            otpOrdRec.OrdNo = Guid.NewGuid().ToString();
            otpOrdRec.OrdTitle = "-";
            otpOrdRec.OrdExp = "-";
            //总金额
            otpOrdRec.TotAmt = input.TotAmt;
            //折扣金额
            otpOrdRec.DiscAmt = input.DiscAmt;
            //预收金额
            otpOrdRec.AdvAmt = input.AdvAmt;
            //应收金额 = 总金额 - 折扣金额 - 预收金额
            otpOrdRec.RecAmt = input.RecAmt;
            //自付金额
            otpOrdRec.SelfpayAmt = otpOrdRec.SelfpayAmt;
            otpOrdRec.OrdStas = EnumOrdStas.未完成;
            otpOrdRec.PayValiTime = DateTime.Now.AddDays(1);
            otpOrdRec = await _otpOrdRecRepository.InsertAsync(otpOrdRec);
            var itemDto = ObjectMapper.Map<OtpOrdRec, OtpOrdRecDto>(otpOrdRec);

            return itemDto;
        }
    }
}
